package com.itheima.aop;

import com.itheima.common.Speed;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;

/**
 * @author :WuJunJie
 * packageName :com.itheima.aop
 * fileName :SpeedAspect
 * creationTime :2022-12-03 17:06
 */
@Aspect
@Component
@Slf4j
public class SpeedAspect {
    @Pointcut("execution(* com.itheima.controller.DishController.*(..))")
    public void pt(){}

    @Around("pt()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        long start = System.currentTimeMillis();
        Object proceed = pjp.proceed();
        Class<?> targetClass = pjp.getTarget().getClass();
        Class[] arguments = new Class[2];
        arguments[0]=String.class;
        arguments[1]=Integer.class;
        Method selectDishes = targetClass.getMethod("selectDishes", arguments);
        boolean annotationPresent = selectDishes.isAnnotationPresent(Speed.class);
        long end = System.currentTimeMillis();
        if (annotationPresent){
            log.info("运行时间：{}ms",end-start);
        }
        return proceed;
    }
}
